//package cn.com.zetatech.loader.resolver.service.impl;
//
//import cn.com.zetatech.loader.entity.FileResolveLog;
//import cn.com.zetatech.loader.entity.ParquetMessage;
//import cn.com.zetatech.loader.entity.RowToColumnTransform;
//import cn.com.zetatech.loader.mapper.ResolverMapper;
//import cn.com.zetatech.loader.resolver.service.ResolverParquetFunction;
//import cn.com.zetatech.loader.util.RegexUtil;
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.IdUtil;
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import com.google.gson.Gson;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.avro.generic.GenericRecord;
//import org.apache.hadoop.conf.Configuration;
//import org.apache.hadoop.fs.Path;
//import org.apache.parquet.avro.AvroParquetReader;
//import org.apache.parquet.hadoop.ParquetReader;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.stereotype.Service;
//
//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.nio.ByteBuffer;
//import java.nio.charset.StandardCharsets;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
///**
// * @author jian.chen@zetatech.com.cn
// * @Description TODO
// * @Date 2024/8/18 21:54
// */
//@Slf4j
//@Service
//@Qualifier("resolveEmsWsVtDistServiceImpl")
//public class ResolveEmsWsVtDistServiceImpl extends ResolverParquetFunction {
//
//    @Autowired
//    private Gson gson;
//
//    @Autowired
//    private ResolverMapper resolverMapper;
//
//    public static String WS_VTDIST = "ws_vtdist";
//
//    @Override
//    public void handle(ParquetMessage message) throws Exception {
//        long startTime = System.currentTimeMillis();
////        if (message.getGenre().isEmpty() || CollectionUtil.isEmpty(message.getFiles())) {
////            return;
////        }
//        message.setGenre("ws_vtdist");
//        if (!WS_VTDIST.equals(message.getGenre())) {
//            return;
//        }
//        //parquetFilePath todo
////        String filePath = message.getFiles().values().iterator().next();
////        FileResolveLog entity = new FileResolveLog();
////        String fileName = RegexUtil.extractFileName(filePath);
////        entity.setFilePath(filePath);
////        entity.setFileName(fileName);
//
////        FileResolveLog resolveLogs = resolverMapper.selectFileResolveLog(entity);
////        if (ObjectUtil.isNotNull(resolveLogs)) {
////            log.info("文件已经被解析: {}", fileName);
////            return;
////        }
//        //todo
//        String parquetFilePath = "C:\\Users\\86130\\Downloads\\3b4f3f7233e68e9f-ems-ws-48853be000000000.parq";
//        String csvFilePath = "C:\\Users\\86130\\Downloads\\output2233.csv";
//
//        log.info("Parsing started at: {}", startTime);
//        String status = StrUtil.EMPTY;
//        long size = 0;
//        try (BufferedWriter writer = new BufferedWriter(new FileWriter(csvFilePath))) {
//            File file = new File(parquetFilePath);
//            Path path = new Path(file.getAbsolutePath());
//            size = file.length();
//            Configuration configuration = new Configuration();
//            ParquetReader<GenericRecord> reader = AvroParquetReader.<GenericRecord>builder(path)
//                    .withConf(configuration)
//                    .build();
//
//            GenericRecord record;
//            boolean headerWritten = false;
//
//            while ((record = reader.read()) != null) {
//                Map<String, Object> dataMap = new HashMap<>();
//                List<Map<String, Object>> dataMapAll = new ArrayList<>();
//
//                // 迭代记录中的每个字段并将其存储在映射中
//                for (String fieldName : record.getSchema().getFields().stream()
//                        .map(field -> field.name()).toArray(String[]::new)) {
//                    Object value = record.get(fieldName);
//
//                    // 处理 ByteBuffer 转换
//                    if (value instanceof ByteBuffer) {
//                        ByteBuffer byteBuffer = (ByteBuffer) value;
//
//                        // 将 ByteBuffer 转换
//                        String stringValue = StandardCharsets.UTF_8.decode(byteBuffer).toString();
//                        dataMap.put(fieldName, stringValue);
//                    } else {
//                        dataMap.put(fieldName, value);
//                    }
//                }
//                log.debug("Read record with {} fields.", dataMap.size());
//                List<Map<String, Object>> transformedRows = RowToColumnTransform.transformRowToColumns(dataMap);
//                dataMapAll.addAll(transformedRows);
//
//                // Write the header (field names) if not written yet
//                if (!headerWritten && !dataMapAll.isEmpty()) {
//                    writer.write(String.join(",", dataMapAll.get(0).keySet()));
//                    writer.newLine();
//                    headerWritten = true;
//                }
//
//                // 写入每个转换行的数据值
//                for (Map<String, Object> row : dataMapAll) {
//                    writer.write(String.join(",", row.values().stream()
//                            .map(String::valueOf)
//                            .toArray(String[]::new)));
//                    writer.newLine();
//                }
//            }
//            reader.close();
//            status = "success";
//            log.info("CSV file written to: {}", csvFilePath);
//
//        } catch (IOException e) {
//            status = "fail";
//            log.error("An error occurred during file processing", e);
//        } finally {
//            long endTime = System.currentTimeMillis();
//            log.info("Parsing completed at: {}", endTime);
//            log.info("Total parsing time: {} milliseconds", endTime - startTime);
//            FileResolveLog fileResolveLog = new FileResolveLog();
//            fileResolveLog.setRunDuration(endTime - startTime);
//            fileResolveLog.setMsg(gson.toJson(message));
//            fileResolveLog.setFilePath(csvFilePath);
////            fileResolveLog.setFileName(fileName);
//            fileResolveLog.setFileSize(size);
//            fileResolveLog.setResultCode(status);
//            this.save(fileResolveLog);
//        }
//    }
//
//    @Override
//    public void save(FileResolveLog fileResolveLog) throws Exception {
//        try {
//            fileResolveLog.setId(IdUtil.objectId());
//            fileResolveLog.setCreateTime(DateUtil.now());
//            resolverMapper.insert(fileResolveLog);
//            log.info("日志插入成功 {}", LocalDateTime.now());
//        } catch (Exception e) {
//            log.error("Failed to save log entry", e);
//        }
//    }
//
//    @Override
//    public FileResolveLog check(FileResolveLog fileResolveLog) throws Exception {
//        return resolverMapper.selectFileResolveLog(fileResolveLog);
//    }
//
//    /**
//     * Checks if the directory of the given file path exists. If not, creates the directory recursively.
//     *
//     * @param filePath The full path to the file, including directories.
//     * @return true if the directory exists or was successfully created, false otherwise.
//     */
//    public static boolean ensureDirectoryExists(String filePath) {
//        try {
//            // Extract the directory path from the file path
//            File file = new File(filePath);
//            File directory = file.getParentFile();
//
//            // Check if the directory exists
//            if (!directory.exists()) {
//                // If the directory doesn't exist, create it recursively
//                return directory.mkdirs();
//            }
//            return true;
//        } catch (SecurityException e) {
//            System.out.println("SecurityException: Insufficient permissions to create the directory.");
//            e.printStackTrace();
//            return false;
//        } catch (Exception e) {
//            System.out.println("An unexpected error occurred.");
//            e.printStackTrace();
//            return false;
//        }
//    }
//
//    public static void main(String[] args) {
//        // Example usage
//        String filePath = "C:\\Users\\86130\\Downloads\\test\\rt\\新建 XLS 工作表.xls";
//        boolean result = ensureDirectoryExists(filePath);
//
//        if (result) {
//            System.out.println("Directory is ready for use.");
//        } else {
//            System.out.println("Failed to ensure the directory exists.");
//        }
//    }
//}
